<?php

    namespace Sale\Controller;

    use Common\Controller\AdminbaseController;
    class CardRechargeController extends AdminbaseController {

        protected $m_card,$m_recharge,$m_logistics,$m_refund;

        function _initialize() {
            parent::_initialize();
            $this->m_card= D("Admin/Card");
            $this->m_refund= D("Admin/Refund");
            $this->m_recharge= D("Admin/Recharge");
            $this->m_logistics= D("Admin/Logistics");
        }

        /**
         * 卡片充值
         */
        public function recharge(){
            $this->assign("sale",get_sale_data());
            $card_list = $this->m_card->where(array("lid"=>get_logistics_id()))->select();

            $this->assign("card_list",$card_list);
            $this->display();
        }

        /*卡片充值*/
        public function recharge_post(){
            $sale_id = get_sale_id();
            $user_id = I("post.user_id");
            $card_number = I("post.card_number");
            $price = I("post.recharge_price");
            $vip_name = I("post.vip_name");
            $vip_phone = I("post.vip_phone");
            $vip_plate_number = I("post.vip_plate_number");
            $desc = I("post.desc");

            $lid = 1;   // 个人账户的物流公司

            $card_update = array(
                'vip_name' => $vip_name,
                'vip_phone' => $vip_phone,
                'vip_plate_number' => $vip_plate_number,
            );

            $card_info = $this->m_card->where(array('number'=>$card_number))->find();
            if ($card_info['lid'] != $lid) {
                // 更换物流公司为个人
                // $this->m_card->where(array('number'=>$card_number))->save(array('lid'=>$lid));
                // 更换物流公司为个人
                // $this->m_logistics->where(array('id'=>$lid))->setInc('total_card_num');
                // if ($card_info['is_active']) {
                //     $this->m_logistics->where(array('id'=>$lid))->setInc('active_card_num');
                // }
                $this->error('物流卡无法在消费机构充值！');
            }

            if(empty($card_number)){
                $this->error('卡号不能为空！');
            }
            if(empty($price)){
                $this->error('充值金额不能为空！');
            }
            $result =  $this->m_card->pre_recharge_price($lid, $card_number, $price, $desc, $sale_id, $user_id);
            if($result === -2){
                $this->error('充值失败，金额不足！');
            }
            if($result === -3){
                $this->error('充值失败，充值金额不足以开卡！');
            }

            $recharge_number = $result['number'];
            $result =  $this->m_recharge->update_status($recharge_number);

            if ($result) {
                $this->m_card->where(array('number'=>$card_number))->save($card_update);
                push_recharge($result['id']);
                redirect("/index.php?g=Logistics&m=AdminCard&a=recharge_print&id=".$result['id']);
            } else {
                $this->error('回调失败！');
            }
        }

        /**
         * 充值账单
         */
        public function recharge_bill(){
            $where = array();
            $where['lid'] =get_logistics_id();
            $where['model'] = 2;
            $count=$this->m_recharge->where($where)->count();
            $where = array();
            $where['r.lid'] =get_logistics_id();
            $where['r.model'] = 2;
            $page = $this->page($count, 15);
            $list = $this->m_recharge->get_card_list($where,"create_time DESC",$page->firstRow, $page->listRows);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        public function recharge_print(){
            $id = I("get.id");
            $recharge_data = $this->m_recharge->where(array("id"=>$id))->find();
            $recharge_data['total_add_point'] = $recharge_data['general_point'] + $recharge_data['self_point'];
            $this->assign("recharge_data",$recharge_data);

            $card_data = $this->m_card->where(array('id'=>$recharge_data['rid']))->find();
            $this->assign("card_data",$card_data);

            $logistics = M("Logistics")->where(array("id"=>$recharge_data['lid']))->find();
            $this->assign("logistics",$logistics);

            $logistics_user = M('users')->where(array('id'=>$recharge_data['uid']))->find();
            $this->assign("logistics_user",$logistics_user);
            
            $this->display();
        }

        /**
         * 卡片退款
         */
        public function refund(){
            $id = I("get.id");
            $card = $this->m_card->get_one(array('c.id'=>$id));
            $this->assign("card",$card);
            if(is_manager($this->user_model)){
                $card_list = $this->m_card->select();
            }else{
                $this->assign("logistics",get_logistics_data());
                $card_list = $this->m_card->where(array("lid"=>get_logistics_id()))->select();
            }

            $this->assign("card_list",$card_list);
            $this->display();
        }

        /**
         * 退款提交
         */
        public function refund_post(){
            if (IS_POST) {
                $card_number = I("post.card_number");
                $price = I("post.refund_price");
                $desc = I("post.desc");

                if(empty($card_number)){
                    $this->error("卡号不能为空！");exit;
                }
                if(empty($price)){
                    $this->error("退款金额不能为空！");exit;
                }
                $card = $this->m_card->get_one(array('c.number'=>$card_number));
                $insert = array(
                    "rid"=>$card["id"],
                    "price"=>$price,
                    "desc"=>$desc
                );
                $result = $this->m_refund->insert_data($insert,2);
                if($result === -1){
                    $this->error("参数有误！");exit;
                }elseif($result === -2){
                    $this->error("金额输入有误！");exit;
                }elseif($result === -3){
                    $this->error("数据为空！");exit;
                }elseif($result === -4){
                    $this->error("操作失败！");exit;
                }elseif($result === -5){
                    $this->error("余额不足！");exit;
                }
                if ($result) {
                     $this->success("退款成功！");
                } else {
                    $this->error("退款失败！");
                }

            }
        }

        /**
         * 退款账单
         */
        public function refund_bill(){
            $where = array();
            $lid = get_logistics_id();
            if($lid){
                $where['r.lid'] =get_logistics_id();
            }

            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00")." -7 day");
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);
            $where["r.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));

            //卡号筛选
            $s_card_number = I("s_card_number");
            if(!empty($s_card_number)){
                $where["c.number"] = array("like","%$s_card_number%");
                $this->assign("s_card_number",$s_card_number);
            }


            $where['r.model'] = 2;
            $count=$this->m_refund->alias("r")->join("bdt_card c ON c.id = r.rid","LEFT")->where($where)->count();

            $page = $this->page($count, 15);
            $list =$this->m_refund->alias("r")->field("r.*,l.logistics_name,c.number AS card_number,u.user_nicename,u.user_phone")->where($where)
                ->join("bdt_card c ON c.id = r.rid","LEFT")
                ->join("bdt_logistics l ON l.id = r.lid","LEFT")
                ->join("bdt_users u ON u.id = r.uid","LEFT")
                ->order("create_time DESC")
                ->limit($page->firstRow , $page->listRows)
                ->select();



            if(I('s_download') == 1){
                $export_title = array(
                    'number'=>'账单号',
                    'card_number'=>'卡号',
                    'price'=>'退款金额',
                    'before_balance'=>'退款前余额',
                    'user_nicename'=>'操作员',
                    'user_phone'=>'操作员手机号',
                    'create_time'=>'创建时间',
                );
                $excel = new \Common\Lib\excel();
                $excel->export_data('卡片退款账单',$export_title,$list);exit;
            }
            foreach($list AS &$item){
                $item["create_time"] = date("Y-m-d H:i:s",$item["create_time"]);
            }
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }
    }